#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/8/26 16:02
# @Author  : 李中华
# @Email   : 1260429766@qq.com
# @File    : basepage.py

from selenium import webdriver
from common.logger import Log
from selenium.webdriver.support.ui import WebDriverWait

from common.readConfig import ReadConfig
from config.confInfo import ConfInfo
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from common.readYAML import ReadYAML
import time

logger = Log()


class BasePage(object):
    def __init__(self):
        option = webdriver.ChromeOptions()
        option.add_argument('--ignore-certificate-errors')
        self.timeout = 10
        self.driver = webdriver.Chrome(options=option)
        self.wait = WebDriverWait(self.driver, timeout=self.timeout)

    def getUrl(self, url):
        logger.info("访问地址{}".format(url))
        self.driver.maximize_window()
        self.driver.set_page_load_timeout(60)
        try:
            self.driver.get(url)
            self.driver.implicitly_wait(10)

        except TimeoutException:
            raise TimeoutException('打开{}超时，请检查网络或者地址是否正确'.format(url))

    def element_locator(self, func, locator):
        name, value = locator
        keys = ConfInfo.ELEMENT_LOCATE_MODE.keys()
        flag = 0
        for key in keys:
            if name == key:
                flag = 1
                name = ConfInfo.ELEMENT_LOCATE_MODE[key]
                return func(name, value)
            else:
                pass
        if flag == 0:
            raise Exception("没有此元素", name)

    def find_element(self, locator):
        return self.element_locator(lambda *args: self.wait.until(EC.presence_of_element_located(args)), locator)

    def find_elements(self, locator):
        return self.element_locator(lambda *args: self.wait.until(EC.presence_of_all_elements_located(args)), locator)

    def input_element_text(self, locator, text):
        self.find_element(locator).clear()
        self.find_element(locator).send_keys(text)
        logger.info("元素{0}，输入文本：{1}".format(str(locator), text))

    def click(self, locator):
        self.find_element(locator).click()
        time.sleep(1)
        logger.info("点击元素：{}".format(str(locator)))
        self.page_title()

    def element_text(self, locator):
        text = self.find_element(locator).text
        logger.info("获取元素{0}文本值：{1}".format(str(locator),text))
        return text

    def page_title(self):
        title = self.driver.title
        current_url = self.driver.current_url
        logger.info("当前页面为{0}，页面title值：{0}".format(current_url,title))
        return current_url,title

    def page_refresh(self):
        front_current_url = self.driver.current_url
        logger.info("当前页面为{}".format(front_current_url))
        self.driver.refresh()
        time.sleep(1)
        back_current_url = self.driver.current_url
        logger.info("刷新后的页面为{}".format(back_current_url))
        return front_current_url,back_current_url

if __name__ == '__main__':
    logur = ReadConfig().getConfValue('mskjWebPlateform', 'logurl')
    logurl = logur + "/#/login?redirect=%2Fdashboard"
    page = BasePage()
    page.getUrl(logurl)

    # page.input_element_text(ReadYAML('admin.yaml')['用户名'], 'admin')
    # page.input_element_text(ReadYAML('admin.yaml')['密码'], 'mskj0417')
